//
//  BRHomeViewController.m
//  MyBaseProject
//
//  Created by 任波 on 2017/9/11.
//  Copyright © 2017年 任波. All rights reserved.
//

#import "BRHomeViewController.h"
#import "BRBannerScrollView.h"
#import "BRScrollTextView.h"
#import "BRHomeCell.h"
#import "BRHomeModel.h"
#import "NSObject+Parse.h"
#import "BRNavigationController.h"

#import "TZBaseViewController.h"
#import "FootballLotteryManagers.h"
#import "DataConfigManager.h"

@interface BRHomeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) BRBannerScrollView *bannerScrollView;
@property (nonatomic, strong) BRScrollTextView *scrollTextView;
@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *homeModelArr;

@end

@implementation BRHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"首页";
    [self setupUI];
    [self loadData];
    [self showAlert];
}

- (void)showAlert {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"本应用只是模拟彩票，可选号，可查询开奖信息，暂不支持购买彩票！" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
    [alert show];
}

- (void)setupUI {
    self.collectionView.hidden = NO;
}

- (void)loadData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"home.plist" ofType:nil];
    NSArray *arr = [[NSArray alloc]initWithContentsOfFile:path];
    NSLog(@"首页数据：%@", arr);
    self.homeModelArr = [BRHomeModel parse:arr];
    [self.collectionView reloadData];
}

- (BRBannerScrollView *)bannerScrollView {
    if (!_bannerScrollView) {
        _bannerScrollView = [[BRBannerScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160) dataSourceArr:@[@"home_lunbo1", @"home_lunbo2", @"home_lunbo3"]];
    }
    return _bannerScrollView;
}

- (BRScrollTextView *)scrollTextView {
    if (!_scrollTextView) {
        NSArray *array = @[@"恭喜135****3245竞彩命中 2.23 万元",
                           @"恭喜136****4232竞彩命中 1.23 万元",
                           @"恭喜131****6435竞彩命中 2.54 万元",
                           @"恭喜157****6254竞彩命中 11.53 万元",
                           @"恭喜187****1245竞彩命中 10.74 万元",
                           @"恭喜176****2375竞彩命中 12 万元",
                           @"恭喜152****1275竞彩命中 11.3 万元",
                           @"恭喜151****5246竞彩命中 1.1 万元",
                           ];
        
        _scrollTextView = [[BRScrollTextView alloc]initWithFrame:CGRectMake(0, 160, SCREEN_WIDTH, 40) textArr:array iconName:@"home_notice"];
        _scrollTextView.backgroundColor = [UIColor whiteColor];
    }
    return _scrollTextView;
}

- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 205)];
        _headerView.backgroundColor = RGB_HEX(0xF1F1F1, 1.0);
        [_headerView addSubview:self.bannerScrollView];
        [_headerView addSubview:self.scrollTextView];
    }
    return _headerView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        // 流水布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        // 设置cell的大小
        layout.itemSize = CGSizeMake(SCREEN_WIDTH / 2 - 2, 70);
        // 设置每一行的间距
        layout.minimumLineSpacing = 3;
        // 设置每个cell的间距
        layout.minimumInteritemSpacing = 2;
        // 设置每组的内边距
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 50, 0);
        // 设置collectionView头视图的大小
        layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 205);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TABBAR_HEIGHT) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        // 注册头视图
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        // 注册cell
        [_collectionView registerClass:[BRHomeCell class] forCellWithReuseIdentifier:@"cell"];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    // 如果是头视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        // 添加头视图
        [header addSubview:self.headerView];
        return header;
    }
    // 如果底部视图
    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        
    }
    return nil;
}


#pragma mark - UICollectionViewDataSource, UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.homeModelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BRHomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = [self.homeModelArr objectAtIndex:indexPath.item];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击了第%ld个", indexPath.row);
    switch (indexPath.row) {
        case 0:
        {
            [self gotoLeTouNumber:lSSQ_lottery]; // 双色球
        }
            break;
        case 1:
        {
            [self gotoLeTouNumber:lDLT_lottery]; // 大乐透
        }
            break;
        case 2:
        {
            [self gotoLeTouNumber:lSenvenStar_lottery]; // 七星彩
        }
            break;
        case 3:
        {
            [self gotoLeTouNumber:lPL3_lottery]; // 排列三
        }
            break;
        case 4:
        {
            [self gotoLeTouNumber:lPL5_lottery]; // 排列五
        }
            break;
        case 5:
        {
            [self gotoLeTouNumber:lT225_lottery]; // 22选5
        }
            break;
        case 6:
        {
            [self gotoLeTouNumber:lT317_lottery]; // 31选7
        }
            break;
        case 7:
        {
            [self gotoLeTouNumber:lT367_lottery]; // 36选7
        }
            break;
        case 8:
        {
            [self gotoFootballcompetition]; // 竞彩足球
        }
            break;
        case 9:
        {
            [self gotoCTZQ]; // 更多好玩彩种
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 竞彩足球
- (void)gotoFootballcompetition {
    FootballLotteryManagers *manager = [[FootballLotteryManagers alloc] initWithPlayType:0];
    [self.navigationController pushViewController:manager animated:YES];
}

#pragma mark 传统足球
- (void)gotoCTZQ {
    // 【传统足球】胜负14场:CTFB14ViewController   任选9场:CTFB9ViewController    6场半全场:CTFB6CBQCViewController   4场进球:CTFB4CJQViewController
    NSString *vcString = @"CTFB4CJQViewController";
    Class class = NSClassFromString(vcString);
    UIViewController *controller = [class new];
    //[self.navigationController pushViewController:controller animated:YES];
    BRNavigationController *nav = [[BRNavigationController alloc]initWithRootViewController:controller];
    [self presentViewController:nav];
//    [self addNavigationWithPresentViewController:controller];
}

#pragma mark - 乐透数字
- (void)gotoLeTouNumber:(NSInteger)lotteryType {
    // 初始化购物车视图控制器
    NSString *vcStr = getLotVCString(lotteryType);
    Class class =  NSClassFromString(vcStr);
    TZBaseViewController *mController = [(TZBaseViewController*)[class alloc] initWithLotter_pk:[NSString stringWithFormat:@"%ld",lotteryType] period:nil requestCode:YES delegate:nil];
    
    BRNavigationController *nav = [[BRNavigationController alloc]initWithRootViewController:mController];
    [self presentViewController:nav];
    
}

- (NSMutableArray *)homeModelArr {
    if (!_homeModelArr) {
        _homeModelArr = [[NSMutableArray alloc]init];
    }
    return _homeModelArr;
}

@end
